package com.drinks.yiniang.ui.fragment.user.order;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.user.order.MyOrderAdapter;
import com.drinks.yiniang.common.listener.OnOrderListChangeCallback;
import com.drinks.yiniang.common.recycler.decoration.MyOrderItemDecoration;
import com.drinks.yiniang.common.help.PayManager;
import com.drinks.yiniang.entity.common.StringJson;
import com.drinks.yiniang.entity.common.StringMsgJson;
import com.drinks.yiniang.entity.event.OrderEvent;
import com.drinks.yiniang.entity.event.PayWXEventBus;
import com.drinks.yiniang.entity.pay.AliPayJson;
import com.drinks.yiniang.entity.pay.WeiXinPayJson;
import com.drinks.yiniang.entity.user.order.MyOrderJson;
import com.drinks.yiniang.io.api.OrderApiIO;
import com.drinks.yiniang.ui.activity.MainActivity;
import com.drinks.yiniang.ui.activity.user.order.OrderDetailsActivity;
import com.drinks.yiniang.ui.activity.user.order.OrderEvaluateActivity;
import com.drinks.yiniang.ui.widget.dialog.PayDialog;
import com.drinks.yiniang.ui.widget.recycler.BaseRecyclerView;
import com.hansen.library.ui.widget.refresh.MSwipeRefreshLayout;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.entity.BaseJson;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.listener.OnSureCancelListener;
import com.hansen.library.ui.fragment.BaseLazyFragment;
import com.hansen.library.ui.widget.dialog.Material2Dialog;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.LogUtils;
import com.hansen.library.utils.StringUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import static com.drinks.yiniang.entity.user.order.MyOrderJson.ITEM_TYPE_ORDER_BOTTOM;
import static com.drinks.yiniang.entity.user.order.MyOrderJson.ITEM_TYPE_ORDER_MIDDLE;
import static com.drinks.yiniang.entity.user.order.MyOrderJson.ITEM_TYPE_ORDER_TOP;
import static com.hansen.library.Constants.TYPE_ORDER_ALREADY_DELIVERED;
import static com.hansen.library.Constants.TYPE_ORDER_ALREADY_EVALUATE;
import static com.hansen.library.Constants.TYPE_ORDER_ALREADY_REFUND;
import static com.hansen.library.Constants.TYPE_ORDER_CANCEL;
import static com.hansen.library.Constants.TYPE_ORDER_PLATEFORM_CANCEL;
import static com.hansen.library.Constants.TYPE_ORDER_REFUNDIND;
import static com.hansen.library.Constants.TYPE_ORDER_REFUND_REFUSE;
import static com.hansen.library.Constants.TYPE_ORDER_SELFT_GET;
import static com.hansen.library.Constants.TYPE_ORDER_WAIT_FOR_DELIVER;
import static com.hansen.library.Constants.TYPE_ORDER_WAIT_FOR_EVALUATE;
import static com.hansen.library.Constants.TYPE_ORDER_WAIT_FOR_PAY;
import static com.hansen.library.Constants.TYPE_ORDER_WAIT_FOR_RECEIPT;

/**
 * 我的订单
 * <p>
 * A simple {@link Fragment} subclass.
 */
public class MyOrderFragment extends BaseLazyFragment implements SwipeRefreshLayout.OnRefreshListener,
        BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.OnItemChildClickListener,
        BaseQuickAdapter.RequestLoadMoreListener, OnSureCancelListener, PayDialog.OnPayListener, PayManager.PayResultListener {

    private final int REQ_ORDER_DETAILS_CODE = 0x01;//订单详情code
    private final int REQ_ORDER_EVALUATE_CODE = 0x02;//订单评价code

    private final int TYPE_CONFIRM_DIALOG_DELETE = 0x0A;//删除订单确认
    private final int TYPE_CONFIRM_DIALOG_CANCEL = 0x0B;//取消订单
    private final int TYPE_CONFIRM_DIALOG_RECEIPT = 0x0C;//确认收货

    //refresh
    private MSwipeRefreshLayout refresh_my_order;
    //列表
    private BaseRecyclerView rcv_my_order;

    private MyOrderAdapter mOrderAdapter;

    private PayManager mPayManager;

    //订单类型, -1全部 0待支付 1已支付 2已发货,待配送 3已送达,待确认 4交易完成，待评价 5已评价 6已取消
    //7退款仲裁（退款中） 9门店自提 10已退款 11拒绝退款 12缺货取消一品脱订单状态
    private String mOrderType;

    private int mPage;
    //当前操作的item下标位置
    private int mCurrentItemPos;

    private boolean isNeedRefresh;

    private OnOrderListChangeCallback onOrderListChangeCallback;

    public MyOrderFragment() {
        // Required empty public constructor
    }

    public static MyOrderFragment newInstance(String type) {
        MyOrderFragment fragment = new MyOrderFragment();

        Bundle bundle = new Bundle();
        bundle.putString(Constants.KeyOrderType, type);

        fragment.setArguments(bundle);

        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            onOrderListChangeCallback = (OnOrderListChangeCallback) context;
        } catch (ClassCastException e) {
            LogUtils.e(context.toString() + "must implement OnCouponChooseListener");
        }
    }

    @Override
    protected int getResID() {
        return R.layout.fragment_my_order;
    }

    @Override
    protected void initView(View mView) {
        refresh_my_order = mView.findViewById(R.id.refresh_my_order);

        rcv_my_order = mView.findViewById(R.id.rcv_my_order);
    }

    @Override
    protected void initData() {
        mContext = getActivity();

        mOrderType = getArguments() != null ? getArguments().getString(Constants.KeyOrderType) : null;

        initAdapter();
    }

    private void initAdapter() {
        mOrderAdapter = new MyOrderAdapter(mContext, new ArrayList<MyOrderJson.MyOrderList>());

        rcv_my_order.setLayoutManager(new LinearLayoutManager(mContext));
        rcv_my_order.addItemDecoration(MyOrderItemDecoration.getItemDecoration(5));

        mOrderAdapter.setOrderListEmpty(getLayoutInflater(), rcv_my_order);
        mOrderAdapter.bindToRecyclerView(rcv_my_order);

        TextView tv_empty_view_go = mOrderAdapter.getEmptyView().findViewById(R.id.tv_empty_view_go);
        tv_empty_view_go.setOnClickListener(this);
    }

    @Override
    protected void listener() {
        refresh_my_order.setOnRefreshListener(this);

        mOrderAdapter.setOnItemClickListener(this);
        mOrderAdapter.setOnItemChildClickListener(this);
        mOrderAdapter.setOnLoadMoreListener(this, rcv_my_order);

        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.tv_empty_view_go://去逛逛
                Intent intent = new Intent(mContext, MainActivity.class);
                intent.putExtra(Constants.KeyJumpToHomePage, true);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    @Override
    protected void lazyLoadData() {
        getOrderList();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Constants.RESULT_CODE_NORMAL) {
            switch (requestCode) {
                case REQ_ORDER_DETAILS_CODE://订单详情返回
                    if(onOrderListChangeCallback != null) {
                        onOrderListChangeCallback.onOrderListChange(mOrderType);
                    }
                    EventBus.getDefault().post(new OrderEvent(OrderEvent.REFRESH_ALL_AND_WAIT_PAY_DELIVER_ORDER));
                    break;
                case REQ_ORDER_EVALUATE_CODE://评价返回
                    EventBus.getDefault().post(new OrderEvent(OrderEvent.REFRESH_ALL_AND_WAIT_EVALUATE_ORDER));
                    getOrderList();
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void onRefresh() {
        getOrderList();
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        MyOrderJson.MyOrderList data = mOrderAdapter.getItem(position);

        mCurrentItemPos = position;

        if (data == null) {
            return;
        }

        switch (adapter.getItemViewType(position)) {
            case ITEM_TYPE_ORDER_TOP://头部
                break;
            case ITEM_TYPE_ORDER_MIDDLE://中间
                break;
            case ITEM_TYPE_ORDER_BOTTOM://底部
                switch (view.getId()) {
                    case R.id.btn_my_order_evaluate://评价
                        doEvaluateButtonClick(data);
                        break;
                    case R.id.btn_my_order_delete://删除或取消
                        doDeleteButtonClick(data);
                        break;
                    case R.id.btn_my_order_pay_or_sure://支付或确认订单
                        doPayButtonClick(data);
                        break;
                    default:
                        break;
                }
            default:
                break;
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        MyOrderJson.MyOrderList data = mOrderAdapter.getItem(position);

        if (data == null) return;

        Intent intent = new Intent(mContext, OrderDetailsActivity.class);
        intent.putExtra(Constants.KeyOrderId, data.getId());
        intent.putExtra(Constants.KeyStatus, data.getStatus());
        startActivityForResult(intent, REQ_ORDER_DETAILS_CODE);
    }

    @Override
    public void onSure(int type, String value) {
        switch (type) {
            case TYPE_CONFIRM_DIALOG_CANCEL://取消
                doOrderCancel();
                break;
            case TYPE_CONFIRM_DIALOG_DELETE://删除
                doOrderDelete();
                break;
            case TYPE_CONFIRM_DIALOG_RECEIPT://确认收货
                doOrderConfirm();
                break;
            default:
                break;
        }
    }

    @Override
    public void onCancel(int type) {

    }

    @Override
    public void onPaySuccess(String msg) {
        doPaySuccess();
    }

    @Override
    public void onPayFail(String errCode, String errMsg) {

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && isNeedRefresh) {
            getOrderList();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isNeedRefresh && getUserVisibleHint()) {
            getOrderList();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(PayWXEventBus event) {
        if (PayWXEventBus.PAY_STATUS_SUCCESS == event.getPayStatus()) {//支付成功
            if (PayManager.PAY_TYPE_ORDER_LIST_PAY.equals(event.getPayPage())) {
                doPaySuccess();
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(OrderEvent event) {
        if (mOrderType == null) return;

        switch (mOrderType) {
            case Constants.TYPE_ORDER_ALL://当前是全部订单
                isNeedRefresh = true;
                break;
            case TYPE_ORDER_WAIT_FOR_PAY://当前是待付款订单
                if(event.getRefreshType() == OrderEvent.REFRESH_ALL_AND_WAIT_PAY_ORDER ||
                        event.getRefreshType() == OrderEvent.REFRESH_ALL_AND_WAIT_PAY_DELIVER_ORDER) {
                    isNeedRefresh = true;
                }
                break;
            case TYPE_ORDER_WAIT_FOR_DELIVER://当前是待配送订单
                if(event.getRefreshType() == OrderEvent.REFRESH_ALL_AND_WAIT_PAY_DELIVER_ORDER ||
                        event.getRefreshType() == OrderEvent.REFRESH_ALL_AND_WAIT_DELIVER_RECEIVED_ORDER) {
                    isNeedRefresh = true;
                }
                break;
            case TYPE_ORDER_WAIT_FOR_RECEIPT://当前是待收货订单
                if(event.getRefreshType() == OrderEvent.REFRESH_ALL_AND_WAIT_DELIVER_RECEIVED_ORDER ||
                        event.getRefreshType() == OrderEvent.REFRESH_ALL_AND_WAIT_RECEIVED_EVALUATE_ORDER) {
                    isNeedRefresh = true;
                }
                break;
            case TYPE_ORDER_WAIT_FOR_EVALUATE://当前是待评价订单
                if(event.getRefreshType() == OrderEvent.REFRESH_ALL_AND_WAIT_RECEIVED_EVALUATE_ORDER ||
                        event.getRefreshType() == OrderEvent.REFRESH_ALL_AND_WAIT_EVALUATE_ORDER) {
                    isNeedRefresh = true;
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onPayClick(int payType) {
        if (mPayManager == null) {
            mPayManager = new PayManager(getActivity());
            mPayManager.setPayResultListener(this);
        }
        switch (payType) {
            case PayManager.PAY_CHANNEL_ALIPAY://支付宝支付
                doPayByAliPay();
                break;
            case PayManager.PAY_CHANNEL_WX://微信支付
                doPayByWeiXinPay();
                break;
            case PayManager.PAY_CHANNEL_WALLET://钱包支付
                doPayByWalletPay();
                break;
            default:
                break;
        }
    }

    @Override
    public void onPayDismiss() {

    }

    @Override
    public void onPayClose() {

    }

    //支付成功
    private void doPaySuccess() {
        EventBus.getDefault().post(new OrderEvent(OrderEvent.REFRESH_ALL_AND_WAIT_PAY_DELIVER_ORDER));
        getOrderList();
    }

    private void initConfirmDialog(int type, String content) {
        Material2Dialog dialog = Material2Dialog.newInstance(type, content);

        dialog.show(getChildFragmentManager(), Constants.TAG_TIPS_DIALOG);
    }

    //支付按钮订单不同状态下的不同点击事件
    private void doPayButtonClick(MyOrderJson.MyOrderList data) {
        if (StringUtils.isEmpty(data.getStatus())) {
            return;
        }

        switch (data.getStatus()) {
            case TYPE_ORDER_WAIT_FOR_PAY://待支付(支付操作)
                PayDialog dialog = PayDialog.newInstance();
                dialog.show(getChildFragmentManager(), Constants.TAG_PAY_DIALOG);
                break;
            case TYPE_ORDER_WAIT_FOR_RECEIPT://待收货(确认收货操作)
            case TYPE_ORDER_ALREADY_DELIVERED://已送达
                initConfirmDialog(TYPE_CONFIRM_DIALOG_RECEIPT, getString(R.string.text_receipt_confirm_mark));
                break;
            case TYPE_ORDER_WAIT_FOR_DELIVER://已支付(查看订单操作)
            case TYPE_ORDER_WAIT_FOR_EVALUATE://交易完成-待评价
            case TYPE_ORDER_ALREADY_EVALUATE://已评价-交易成功
            case TYPE_ORDER_CANCEL://已取消
            case TYPE_ORDER_REFUNDIND://退款中
            case TYPE_ORDER_SELFT_GET://门店自提
            case TYPE_ORDER_ALREADY_REFUND://已退款
            case TYPE_ORDER_REFUND_REFUSE://拒绝退款
            case TYPE_ORDER_PLATEFORM_CANCEL://平台取消
            default:
                break;
        }
    }

    //删除按钮订单不同状态下的不同点击事件
    private void doDeleteButtonClick(MyOrderJson.MyOrderList data) {
        if (StringUtils.isEmpty(data.getStatus())) {
            return;
        }

        switch (data.getStatus()) {
            case TYPE_ORDER_WAIT_FOR_PAY://待支付(取消订单操作)
                initConfirmDialog(TYPE_CONFIRM_DIALOG_CANCEL, getString(R.string.text_order_cancel_mark));
                break;
            case TYPE_ORDER_ALREADY_EVALUATE://已评价-交易成功
            case TYPE_ORDER_CANCEL://已取消
            case TYPE_ORDER_ALREADY_REFUND://已退款
            case TYPE_ORDER_REFUND_REFUSE://拒绝退款
            case TYPE_ORDER_PLATEFORM_CANCEL://平台取消
                initConfirmDialog(TYPE_CONFIRM_DIALOG_DELETE, getString(R.string.text_order_delete_mark));
                break;
            case TYPE_ORDER_WAIT_FOR_EVALUATE://交易完成-待评价
            case TYPE_ORDER_WAIT_FOR_DELIVER://已支付
            case TYPE_ORDER_WAIT_FOR_RECEIPT://待收货
            case TYPE_ORDER_ALREADY_DELIVERED://已送达
            case TYPE_ORDER_REFUNDIND://退款中
            case TYPE_ORDER_SELFT_GET://门店自提
                break;
            default:
                break;
        }
    }

    //评价按钮订单不同状态下的不同点击事件
    private void doEvaluateButtonClick(MyOrderJson.MyOrderList data) {
        if (StringUtils.isEmpty(data.getStatus())) {
            return;
        }

        switch (data.getStatus()) {
            case TYPE_ORDER_WAIT_FOR_EVALUATE://交易完成-待评价(订单评价)
                Intent intent = new Intent(mContext, OrderEvaluateActivity.class);
                intent.putExtra(Constants.KeyOrderId, data.getId());
                startActivityForResult(intent, REQ_ORDER_EVALUATE_CODE);
                break;
            case TYPE_ORDER_WAIT_FOR_DELIVER://已支付
            case TYPE_ORDER_WAIT_FOR_PAY://待支付(取消订单操作)
            case TYPE_ORDER_ALREADY_EVALUATE://已评价-交易成功
                ToastUtils.showShort("晒单");
                break;
            case TYPE_ORDER_CANCEL://已取消
            case TYPE_ORDER_REFUNDIND://退款中
            case TYPE_ORDER_SELFT_GET://门店自提
            case TYPE_ORDER_ALREADY_REFUND://已退款
            case TYPE_ORDER_REFUND_REFUSE://拒绝退款
            case TYPE_ORDER_PLATEFORM_CANCEL://平台取消
            default:
                break;
        }
    }

    //订单取消
    private void doOrderCancel() {
        MyOrderJson.MyOrderList data = mOrderAdapter.getItem(mCurrentItemPos);

        if (data == null) {
            ToastUtils.showShort(R.string.text_the_order_not_exist);
            return;
        }

        showLoadingDialog();

        OrderApiIO.getInstance().doOrderCancel(data.getId(), new APIRequestCallback<BaseJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(BaseJson res) {
                EventBus.getDefault().post(new OrderEvent(OrderEvent.REFRESH_ALL_ORDER));
                getOrderList();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    //订单删除
    private void doOrderDelete() {
        MyOrderJson.MyOrderList data = mOrderAdapter.getItem(mCurrentItemPos);

        if (data == null) {
            ToastUtils.showShort(R.string.text_the_order_not_exist);
            return;
        }

        showLoadingDialog();

        OrderApiIO.getInstance().doOrderDelete(data.getId(), new APIRequestCallback<StringJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(StringJson res) {
                if(!Constants.TYPE_ORDER_ALL.equals(mOrderType)) {
                    EventBus.getDefault().post(new OrderEvent(OrderEvent.REFRESH_ALL_ORDER));
                }
                getOrderList();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    //订单确认
    private void doOrderConfirm() {
        MyOrderJson.MyOrderList data = mOrderAdapter.getItem(mCurrentItemPos);

        if (data == null) {
            ToastUtils.showShort(R.string.text_the_order_not_exist);
            return;
        }

        showLoadingDialog();

        OrderApiIO.getInstance().doOrderConfirm(data.getId(), new APIRequestCallback<BaseJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(BaseJson res) {
                EventBus.getDefault().post(new OrderEvent(OrderEvent.REFRESH_ALL_AND_WAIT_EVALUATE_ORDER));
                getOrderList();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    //支付宝支付
    private void doPayByAliPay() {
        MyOrderJson.MyOrderList data = mOrderAdapter.getItem(mCurrentItemPos);

        if (data == null) {
            ToastUtils.showShort(R.string.text_the_order_not_exist);
            return;
        }

        showLoadingDialog();

        OrderApiIO.getInstance().doPayByAliPay(data.getOrder_no(), new APIRequestCallback<AliPayJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(AliPayJson res) {
                mPayManager.slingAiPay(res.getData());
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    //微信支付
    private void doPayByWeiXinPay() {
        MyOrderJson.MyOrderList data = mOrderAdapter.getItem(mCurrentItemPos);

        if (data == null) {
            ToastUtils.showShort(R.string.text_the_order_not_exist);
            return;
        }

        showLoadingDialog();

        OrderApiIO.getInstance().doPayByWeiXin(data.getOrder_no(), new APIRequestCallback<WeiXinPayJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(WeiXinPayJson res) {
                mPayManager.slingWXPay(res.getData(), PayManager.PAY_TYPE_ORDER_LIST_PAY);
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    //钱包支付
    private void doPayByWalletPay() {
        MyOrderJson.MyOrderList data = mOrderAdapter.getItem(mCurrentItemPos);

        if (data == null) {
            ToastUtils.showShort(R.string.text_the_order_not_exist);
            return;
        }

        showLoadingDialog();

        OrderApiIO.getInstance().doPayByWallet(data.getOrder_no(), new APIRequestCallback<AliPayJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(AliPayJson res) {
                doPaySuccess();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    @Override
    public void onLoadMoreRequested() {
        OrderApiIO.getInstance().getOrderList(mOrderType, mPage, new APIRequestCallback<MyOrderJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(MyOrderJson res) {
                dealOrderList(res.getData().getWait_order(), res.getData().getData(), true);
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
                mOrderAdapter.loadMoreFail();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    //获取订单列表
    private void getOrderList() {
        if (StringUtils.isEmpty(mOrderType)) {
            ToastUtils.showShort(getString(R.string.text_order_type_error));
            return;
        }

        if (!refresh_my_order.isRefreshing()) {
            showLoadingDialog();
        }

        mPage = 1;

        OrderApiIO.getInstance().getOrderList(mOrderType, mPage, new APIRequestCallback<MyOrderJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(MyOrderJson res) {
                dealOrderList(res.getData().getWait_order(), res.getData().getData(), false);
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
                mOrderAdapter.loadMoreFail();
            }

            @Override
            public void onComplete() {
                isNeedRefresh = false;
                refresh_my_order.setRefreshing(false);
                dismissDialog();
            }
        });
    }

    //获取到原始订单数据处理
    private void dealOrderList(List<MyOrderJson.MyOrderList> waitPayList, List<MyOrderJson.MyOrderList> list, boolean isLoadMore) {
        if (mOrderAdapter == null) {
            return;
        }

        List<MyOrderJson.MyOrderList> tempList = new ArrayList<>();

        MyOrderJson.MyOrderList mData;

        try {
            if (!CommonUtils.isEmptyList(waitPayList) && mPage == 1) {//第一页数据，处理待支付订单
                for (int i = 0; i < waitPayList.size(); i++) {
                    //拆解top
                    mData = waitPayList.get(i);
                    mData.setItemType(ITEM_TYPE_ORDER_TOP);

                    tempList.add(mData);

                    //拆解middle
                    for (int j = 0; j < waitPayList.get(i).getGoods().size(); j++) {
                        mData = (MyOrderJson.MyOrderList) mData.clone();

                        mData.setTempGoods(mData.getGoods().get(j));
                        mData.setItemType(ITEM_TYPE_ORDER_MIDDLE);

                        tempList.add(mData);
                    }

                    //拆解bottom
                    mData = (MyOrderJson.MyOrderList) mData.clone();
                    mData.setItemType(ITEM_TYPE_ORDER_BOTTOM);
                    tempList.add(mData);
                }
            }

            for (int i = 0; i < list.size(); i++) {
                //拆解top
                mData = list.get(i);
                mData.setItemType(ITEM_TYPE_ORDER_TOP);

                tempList.add(mData);

                //拆解middle
                for (int j = 0; j < list.get(i).getGoods().size(); j++) {
                    mData = (MyOrderJson.MyOrderList) mData.clone();

                    mData.setTempGoods(mData.getGoods().get(j));
                    mData.setItemType(ITEM_TYPE_ORDER_MIDDLE);

                    tempList.add(mData);
                }

                //拆解bottom
                mData = (MyOrderJson.MyOrderList) mData.clone();
                mData.setItemType(ITEM_TYPE_ORDER_BOTTOM);
                tempList.add(mData);
            }
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }

        mPage++;

        if (!isLoadMore) {
            mOrderAdapter.getData().clear();
        }

        mOrderAdapter.addData(tempList);

        if (CommonUtils.isHasMoreData(list)) {
            mOrderAdapter.loadMoreComplete();
        } else {
            mOrderAdapter.loadMoreEnd();
        }

        list.clear();
        waitPayList.clear();
    }

    @Override
    public void onDestroy() {
        if(EventBus.getDefault().isRegistered(this)) EventBus.getDefault().unregister(this);
        if (mPayManager != null) {
            mPayManager.destroyPayRegister();
        }
        super.onDestroy();
    }
}
